// src/main.js
import { createApp } from 'vue'
import { ElIcon } from 'element-plus'
import { Search } from '@element-plus/icons-vue'


import App from './App.vue'
import router from './router'
import ElementPlus, { ElMessage } from 'element-plus'
import 'element-plus/dist/index.css'
import axios from 'axios'
import Particles from 'particles.vue3'

// ==== 1. Axios 全局配置 ====
axios.defaults.baseURL = 'http://localhost:9000'
axios.defaults.withCredentials = true  // 如果后端需要携带 Cookie

// 请求拦截器：给每个请求都加上 token
axios.interceptors.request.use(
    config => {
      const token = localStorage.getItem('token')
      if (token) {
        config.headers['token'] = token
      }
      return config
    },
    err => Promise.reject(err)
)

// 响应拦截器：统一处理 ResponseBean 结构
axios.interceptors.response.use(
    response => {
        if (response.config.responseType === 'blob') {
            return response.data;
        }

      // 后端返回的 ResponseBean<T> 结构
      const { status, msg, data } = response.data

      if (status !== 200) {
        // 业务错误
        ElMessage.error(msg || '服务器返回了错误状态')
        return Promise.reject(new Error(msg || 'Error'))
      }

      // 业务成功，直接给到业务层 data????
      return data
    },
    error => {
      // 网络或服务器异常
      ElMessage.error(error.message || '网络或服务器错误')
      return Promise.reject(error)
    }
)

// ==== 2. 挂载 App ====
const app = createApp(App)


app.use(ElementPlus)
    .use(router)
    // 如果你想在 setup 里通过 inject 使用 axios：
    .provide('axios', axios)
    .mount('#app')
app.use(Particles, {
    /* 下面是示例配置，你可以去 tsParticles 官网调试器按需修改 */
    particles: {
        number: { value: 80 },
        color: { value: "#ffffff" },
        shape: { type: "circle" },
        opacity: { value: 0.5 },
        size: { value: { min: 1, max: 3 } },
        move: { enable: true, speed: 2 },
        links: { enable: true, distance: 150, color: "#ffffff", opacity: 0.4, width: 1 }
    },
    interactivity: {
        events: { onhover: { enable: true, mode: "repulse" } }
    }
})
app.component('ElIcon', ElIcon)
app.component('Search', Search)
// main.js
window.addEventListener('error', e => {
    if (
        e.message &&
        e.message.includes('ResizeObserver loop completed with undelivered notifications')
    ) {
        e.stopImmediatePropagation();
    }
});
